from lib.config.base import BaseConfig
from utils import common


class ServerConfig(BaseConfig):
    def __init__(self, path):
        super().__init__(path)

    def is_debug(self):
        debug = common.get_data_value(self.__config, "debug")
        return debug == 1

    def is_startup(self):
        startup = common.get_data_value(self.__config, "startup")
        return startup == 1

    def get_root(self):
        root = common.get_data_value(self.__config, "root")

        if not root:
            root = "/www"
        return root

    def get_projects(self):
        projects = common.get_data_value(self.__config, "projects")
        if not projects:
            return []
        return projects

    def get_default_project(self):
        projects = self.get_projects()
        for pro in projects:
            if "default" in pro:
                return pro
        # 如果没有配置，默认root路径
        return {"name": "default", "path": ""}

    def get_port(self):
        port = common.get_data_value(self.__config, "port")

        if not port:
            port = 80
        return port

    def get_protocol(self):
        protocol = common.get_data_value(self.__config, "protocol")

        if not protocol:
            protocol = "http"
        return protocol
